
package com.shop.cloud.mall.api.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.cloud.common.data.tenant.TenantContextHolder;
import com.shop.cloud.mall.api.mapper.DistributionUserMapper;
import com.shop.cloud.mall.api.service.DistributionConfigService;
import com.shop.cloud.mall.api.service.DistributionUserService;
import com.shop.cloud.mall.api.service.UserRecordService;
import com.shop.cloud.mall.common.constant.MallConstants;
import com.shop.cloud.mall.common.entity.DistributionConfig;
import com.shop.cloud.mall.common.entity.DistributionUser;
import com.shop.cloud.mall.common.entity.UserRecord;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.boot.autoconfigure.klock.model.LockTimeoutStrategy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * 分销员
 *
 * @author
 * @date 2021-04-25 17:40:35
 */
@Service
@AllArgsConstructor
public class DistributionUserServiceImpl extends ServiceImpl<DistributionUserMapper, DistributionUser> implements DistributionUserService {
	private final DistributionConfigService distributionConfigService;
	private final UserRecordService userRecordService;

	@Override
	public IPage<DistributionUser> page1(IPage<DistributionUser> page, DistributionUser distributionUser) {
		return baseMapper.selectPage1(page, distributionUser);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Klock(name = "updateCommissionTotalDistributionUser", keys = {"#userId"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
	public void updateCommissionTotal(String userId, BigDecimal commission) {
		baseMapper.update(new DistributionUser(), Wrappers.<DistributionUser>lambdaUpdate()
				.eq(DistributionUser::getUserId, userId)
				.setSql(" commission_total = commission_total + " + commission));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	@Klock(name = "updateCommissionWithdrawalDistributionUser", keys = {"#userId"}, lockTimeoutStrategy = LockTimeoutStrategy.FAIL_FAST)
	public void updateCommissionWithdrawal(String userId, BigDecimal commission) {
		baseMapper.update(new DistributionUser(), Wrappers.<DistributionUser>lambdaUpdate()
				.eq(DistributionUser::getUserId, userId)
				.setSql(" commission_withdrawal = commission_withdrawal + " + commission));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public DistributionUser getDistributionUser(String userId) {
		DistributionUser distributionUser = baseMapper.selectById(userId);
		//如果用户还不是分销员并满足成为分销员的条件，自动将用户设为分销员
		if(distributionUser == null){
			//获取分销配置
			DistributionConfig distributionConfig = distributionConfigService.getByTenantId(TenantContextHolder.getTenantId());
			if(distributionConfig != null){
				if(MallConstants.DISTRIBUTION_MODEL_2.equals(distributionConfig.getDistributionModel())){//人人分销模式，将用户设为分销员
					distributionUser = new DistributionUser();
					distributionUser.setUserId(userId);
					distributionUser.setCommissionTotal(BigDecimal.ZERO);
					distributionUser.setCommissionWithdrawal(BigDecimal.ZERO);
					baseMapper.insert(distributionUser);
				}
				if(MallConstants.DISTRIBUTION_MODEL_3.equals(distributionConfig.getDistributionModel())){//满额分销模式，判断用户累计消费额度是否达到指定额度，如达到则将用户设为分销员
					//获取用户的消费记录
					UserRecord userRecord = userRecordService.getById(userId);
					if(userRecord != null && userRecord.getTotalAmount().compareTo(distributionConfig.getFullAmount()) >= 0){//用户累计消费额度大于等于指定额度
						//将用户设为分销员
						distributionUser = new DistributionUser();
						distributionUser.setUserId(userId);
						distributionUser.setCommissionTotal(BigDecimal.ZERO);
						distributionUser.setCommissionWithdrawal(BigDecimal.ZERO);
						baseMapper.insert(distributionUser);
					}
				}
			}
		}
		return distributionUser;
	}

}
